package com.example.model_service.controller;

import cn.hutool.core.lang.UUID;
import com.example.model_service.domain.User;
import com.example.model_service.dto.LoginDTO;
import com.example.model_service.dto.UpdateDTO;
import com.example.model_service.service.UserService;
import com.example.model_service.utils.ImgCodeUtil;
import com.example.model_service.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api("用户管理功能")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private ImgCodeUtil imgCodeUtil;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO){
        System.out.println(loginDTO);
        return userService.login(loginDTO);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody LoginDTO loginDTO){
        return userService.register(loginDTO);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/update")
    public Result update(@RequestBody UpdateDTO updateDTO){
        return userService.updateUser(updateDTO);
    }

    @ApiOperation("上传头像图片")
    @PostMapping("/uploadImg")
    public Result upload(@RequestParam("file")MultipartFile file, @RequestParam("userId")Integer userId){
        return userService.uploadImg(file, userId);
    }

    @ApiOperation("生成登录二维码")
    @GetMapping("/getImg")
    public Result getQRCode() {
        // 生成随机登录标识符，据此生成二维码图片
        String qrCodeUrl = imgCodeUtil.generateQRCode(UUID.randomUUID().toString());
        return Result.success("data:image/png;base64,"+qrCodeUrl).message("二维码生成成功");
    }

    @GetMapping("/test")
    public String test(){
        return "ok";
    }

}
